<template>
    <div>
        <el-form ref="ruleFormRef" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm"
            status-icon>
            <el-form-item label="类目名称" prop="name">
                <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="类目图片" prop="img">
                <Tuku v-model="formData.img"></Tuku>
            </el-form-item>
            <el-form-item label="类目排序" prop="seq">
                <el-input-number v-model="formData.seq" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="父目录ID" prop="parentId">

                <el-tree-select v-model="formData.parentId" :data="categorytreedata" check-strictly
                    :render-after-expand="false" />
            </el-form-item>
            <el-form-item label="添加人" prop="lastUpdateBy">
                <el-input v-model="formData.lastUpdateBy" />
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    提交
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import Tuku from '../../components/TuKu.vue'
import { CategoryApi } from '@/api'
const ruleFormRef = ref()
import { ElMessage } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()

//添加表单的属性定义
const formData = reactive({
    id:'',
    name: '',
    img: '',
    seq: 0,
    parentId: 0,
    lastUpdateBy: ''
})

//添加表单的校验规则定义

const rules = reactive({
    name: [
        { required: true, message: '请输入2to10个字符', trigger: 'blur' },
        { min: 2, max: 10, message: '字符输入规则是 2 to 10 个', trigger: 'blur' },
    ],
})
//树形选择器
const categorytreedata = ref([
    {
        value: 0,
        label: '一级目录',
    },
])
onMounted(() => {
    callCategoryApi()
    console.log(route.query.id)
   formData.id = String(route.query.id)
   cellcategorybyid()
})
const testhezi = () => {
    let hzh = ['口红', '首饰', '晒红', '粉扑', '粉底液']
    const cp = hzh.reduce((hezi: any, currentvalue: any) => {
        if (currentvalue === '口红') { hezi.push(currentvalue) }
        if (currentvalue === '粉底液') { hezi.push(currentvalue) }
        return hezi
    }, [])
    console.log(cp);

}
//数据回显查询接口
const cellcategorybyid = () => {
    CategoryApi.select.cell({id:formData.id}).then((res:any)=>{
        console.log(res);
        formData.name = res[0].name
        formData.img = res[0].img
        formData.seq = res[0].seq
        formData.parentId = res[0].parentId
        formData.lastUpdateBy = res[0].lastUpdateBy
    })
}


//api调用 调用分类数据
const callCategoryApi = () => {
    CategoryApi.select.cell().then((res: any) => {
        res.forEach((item: any) => {
            item.value = item.id
            item.label = item.name
        });
        const categorydata = res.reduce((hezi: any, currentvalue: any) => {
            currentvalue.children = res.filter((item: any) => item.parentId === currentvalue.id)
            if (currentvalue.parentId === 0) {
                hezi.push(currentvalue)
            }
            return hezi
        }, [])
        console.log(categorydata);
        categorytreedata.value = categorydata
    })
}

const formsubmit = () => {
    CategoryApi.update.cell({
        id: formData.id,
        name: formData.name,
        img: formData.img,
        seq: formData.seq,
        parentId: formData.parentId,
        lastUpdateBy: formData.lastUpdateBy
    }).then((res: any) => {
        ElMessage.success('修改成功!', res.msg)
        //跳转到列表页面
        router.push({ name: 'category-list' })
        //更新列表
        callCategoryApi()
    })
}


//表单提交
const submitForm = async (formEl: any) => {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            console.log('submit!')
            formsubmit()
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: any) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>

<style scoped>
.el-select {
    width: 160px;
}
</style>